package com.iwhalecloud.bss.kite.cucc.service.scene.query;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQueryFifthGenerationService;
import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.dto.inst.OfferInstDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.LabelOfferDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.MemberRightOfferDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.OfferDTO;
import com.iwhalecloud.bss.kite.client.dto.spec.OfferRoleExtDTO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.constant.LabelConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.validate.annotation.KiteNotBlank;
import com.iwhalecloud.bss.kite.dataservice.bo.impl.QueryMainOfferInstBO;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.inst.OfferInst;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.OfferDetail;
import com.iwhalecloud.bss.kite.dataservice.mapper.inst.OfferInstMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.FifthGenMapper;
import com.iwhalecloud.bss.kite.dataservice.mapper.spec.OfferRelMapper;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.ztesoft.bss.base.staticdata.CoreDataOffer;
import com.ztesoft.bss.order.base.unicode.UnicodeProd;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <Description> 5G专区查询相关 <br>
 *
 * @author huang.caibiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @date 2019-11-01 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.service.scene.query <br>
 */
@Service("queryFifthGenerationService")
public class QueryFifthGenerationService implements IQueryFifthGenerationService {

    @Autowired
    private FifthGenMapper fifthGenMapper;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Autowired
    private QueryMainOfferInstBO queryMainOfferInstBO;

    @Autowired
    private OfferInstMapper offerInstMapper;

    @Autowired
    private OfferRelMapper offerRelMapper;


    @Override
    public DataResult<Map<String, List<MemberRightOfferDTO>>> listMemberRightOffers(@KiteNotBlank String offerId) {
        //100000:可选依赖  500000 :  推荐
        List<MemberRightOfferDTO> offers = fifthGenMapper.listMemberRightOffers(offerId, CoreDataOffer.OFF_0006_100000);
        Map<String, List<MemberRightOfferDTO>> offerGroup = offers.stream()
            .collect(Collectors.groupingBy(MemberRightOfferDTO::getOfferGrpId));

        return ErrorConsts.SUCCESS.getResult(offerGroup);

    }

    @Override
    public DataResult<List<LabelOfferDTO>> listContractOffers(@KiteNotBlank String offerId) {
        return ErrorConsts.SUCCESS.getResult(fifthGenMapper.listLabelOffers(offerId, LabelConsts.FIFTH_GEN_CONTRACT_OFFER));
    }

    @Override
    public DataResult<List<LabelOfferDTO>> listVoiceOffers(@KiteNotBlank String offerId) {
        return ErrorConsts.SUCCESS.getResult(fifthGenMapper.listLabelOffers(offerId, LabelConsts.FIFTH_GEN_VOICE_OFFER));
    }

    @Override
    public DataResult<Map<String, List<MemberRightOfferDTO>>> listPreferentialOffers(@KiteNotBlank String offerId) {
        //100000:可选依赖  500000 :  推荐
        List<MemberRightOfferDTO> offers = fifthGenMapper.listMemberRightOffers(offerId, CoreDataOffer.OFF_0006_500000);
        Map<String, List<MemberRightOfferDTO>> offerGroup = offers.stream()
            .collect(Collectors.groupingBy(MemberRightOfferDTO::getOfferGrpId));

        return ErrorConsts.SUCCESS.getResult(offerGroup);
    }

    @Override
    public DataResult<List<OfferDTO>> listFifthGenOffers(String offerId, String mainOfferId) {
        List<OfferDTO> offerDTOList = fifthGenMapper.listFifthGenOffers(LabelConsts.FIFTH_GEN_MAIN_OFFER);
        if (KiteListUtils.isEmpty(offerDTOList)) {
            return ErrorConsts.SUCCESS.getResult(Collections.emptyList());
        }

        if (StringUtils.isNotBlank(offerId) && CoreDataOffer.OFF_0005_13.equals(OfferCache.get(offerId).getOfferType())) {
            //如果是融合
            if (StringUtils.isNotBlank(mainOfferId)) {
                //融合下面的主套餐迁转
                List<OfferRoleExtDTO> offerRoleExtDTOList = offerRelMapper.listMainOfferOfPromotionOffer(offerId, mainOfferId);
                offerDTOList = offerDTOList.stream().filter(offerDTO -> offerRoleExtDTOList.stream().map(OfferRoleExtDTO::getOfferId).collect(Collectors.toList()).contains(offerDTO.getOfferId())).collect(Collectors.toList());
            }
            else {
                //融合迁转
                offerDTOList = offerDTOList.stream().filter(offerDTO -> CoreDataOffer.OFF_0005_13.equals(offerDTO.getOfferType())).collect(Collectors.toList());
            }

        }
        else if (StringUtils.isNotBlank(mainOfferId)) {
            //如果是主套餐 直接返回主套餐
            offerDTOList = offerDTOList.stream().filter(offerDTO -> CoreDataOffer.OFF_0005_11.equals(offerDTO.getOfferType())).collect(Collectors.toList());
        }

        return ErrorConsts.SUCCESS.getResult(offerDTOList);

    }

    @Override
    public DataResult<List<OfferDTO>> listTrafficOffers(@KiteNotBlank String custId, @KiteNotBlank String prodInstId) {
        OfferInstDTO kiteOfferInst = queryMainOfferInstBO.getMainOfferInstByProdInstId(prodInstId, custId);
        List<LabelOfferDTO> offerDTOList = fifthGenMapper.listLabelOffers(kiteOfferInst.getOfferId(), LabelConsts.FIFTH_GEN_UP_ADDITIONAL_OFFER);
        if (KiteListUtils.isNotEmpty(offerDTOList)) {
            List<KiteOfferInst> offerInstList = querySceneInstService.listAdditionalOfferInstsFromMainOfferInst(custId, kiteOfferInst.getOfferInstId());
            if (offerInstList.stream().anyMatch(offerInst -> offerDTOList.stream().anyMatch(offerDTO -> StringUtils.equals(offerDTO.getOfferId(), offerInst.getOfferId())))) {
                ErrorConsts.REPEAT_ORDER_OFFER.throwOut();
            }

        }

        return ErrorConsts.SUCCESS.getResult(KiteBeanUtils.copyPropsForList(offerDTOList, OfferDTO.class));
    }


    @Override
    public DataResult<List<OfferDTO>> listVipMemberOffers() {
        return ErrorConsts.SUCCESS.getResult(fifthGenMapper.listFifthGenOffers(LabelConsts.FIFTH_GEN_MEMBER_OFFER));
    }

    @Override
    public DataResult<OfferInstDTO> getMainOfferInstByProdInstId(@KiteNotBlank String custId, @KiteNotBlank String prodInstId) {
        return ErrorConsts.SUCCESS.getResult(queryMainOfferInstBO.getMainOfferInstByProdInstId(prodInstId, custId));
    }

    @Override
    public DataResult<List<String>> listAdditionalOfferInsts(@KiteNotBlank String custId,
        @KiteNotBlank String prodInstId) {
        List<OfferInst> additionalOfferInsts = offerInstMapper.listAllAdditionalOfferInstByProdInstId(prodInstId, custId);
        return ErrorConsts.SUCCESS.getResult(additionalOfferInsts.stream().map(OfferInst::getOfferId).distinct().collect(Collectors.toList()));
    }

    @Override
    public DataResult<String> getMainOfferByPromotionOffer(@KiteNotBlank String offerId) {
        List<OfferRoleExtDTO> offerRoleExtDTOList = offerRelMapper.listMainOfferOfPromotionOffer(offerId, null);
        String mainOfferId = null;
        for (OfferRoleExtDTO offerRoleExtDTO : offerRoleExtDTOList) {
            List<OfferDetail> offerDetailList = OfferCache.get(offerRoleExtDTO.getOfferId()).listAccessProdDetail();
            if (offerDetailList.stream().anyMatch(offerDetail -> offerDetail.getObjId().equals(UnicodeProd.PROD_100000))) {
                mainOfferId = offerRoleExtDTO.getOfferId();
                break;
            }

        }
        return ErrorConsts.SUCCESS.getResult(mainOfferId);
    }

    @Override
    public DataResult<List<LabelOfferDTO>> listLabelOffersByCode(@KiteNotBlank List<String> labelCodes) {
        return ErrorConsts.SUCCESS.getResult(fifthGenMapper.listLabelOffersByCode(labelCodes));
    }

}
